package com.easou.ps.sort;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import com.easou.ps.thrift.search.types.ChannelDataType;
import com.easou.ps.utils.URLTools;
import com.easou.ps.webmaster.SearchResponse;
import com.easou.ps.webmaster.SearchResult;

/**
 * 插入整合结果集到结果队列中
 * @author dirk
 *	
 */
public class InsertChannelResult {
	public static void todo(SearchResponse response, List<SearchResult> channelSearchResults) {
		try {
			if (response.getResults()==null) {
				response.setResults(new ArrayList<SearchResult>());
			}
			int last3gPos = 0;

			List<SearchResult> results = response.getResults();
			if (channelSearchResults == null) {
				return ;
			}
			
			SearchResult tvcat = null;
			SearchResult qitan = null;
			SearchResult film = null;
			SearchResult teleplay = null;//视频放在第一位
//			SearchResult gouwu = null;
			SearchResult stock = null;
			int tqf = 0;
			// 如果有整合出来的小说（book.easou.com），就把检索出来的小说去掉(b.easou.com)
			for (int i = channelSearchResults.size() - 1; i >= 0; i--) {
				Map<String, String> extendFields = channelSearchResults.get(i).getExtendFields();
				if ("book.easou.com".equals(extendFields.get("host"))){
					for (int j = results.size() - 1; j >= 0; j--) {
						SearchResult result = results.get(j);
						if(result.getUrl()!=null){
							String host = URLTools.getHostFromURL2(result.getUrl());
							if ("b.easou.com".equals(host) || ("book.easou.com".equals(host))) {
								results.remove(j);
							}
						}
					}
				}else if ("appClassify".equals(extendFields.get("type"))) {
					for (int j = results.size() - 1; j >= 0; j--) {
						SearchResult result = results.get(j);
						if("vert_app".equals(result.getType())){							
							results.remove(j);
						}
					}
				}
			}

			// 当出现电影网、电视剧、奇谈电影时，按电影网、电视剧、奇谈电影顺序1、3、5排列
			for (int i = channelSearchResults.size() - 1; i >= 0; i--) {
				Map<String, String> extendFields = channelSearchResults.get(i).getExtendFields();
				if (ChannelDataType.tvcat.name().equals(extendFields.get("type"))) {
					tqf++;
				} else if (ChannelDataType.qitan.name().equals(extendFields.get("type"))) {
					tqf++;
				} else if (ChannelDataType.film.name().equals(extendFields.get("type"))) {
					tqf++;
//				} else if (ChannelDataType.gouwu.name().equals(extendFields.get("type"))) {
//					gouwu = channelResults.remove(i);
				} else if (ChannelDataType.stock.name().equals(extendFields.get("type")) && !"true".equals(extendFields.get("isCode"))) {
					stock = channelSearchResults.remove(i);
				} else if ("teleplayDetail".equals(extendFields.get("type"))) {
					teleplay = channelSearchResults.remove(i);
				}
			}
			if (tqf > 1) {
				for (int i = channelSearchResults.size() - 1; i >= 0; i--) {
					Map<String, String> extendFields = channelSearchResults.get(i).getExtendFields();
					if (ChannelDataType.tvcat.name().equals(extendFields.get("type"))) {
						tvcat = channelSearchResults.remove(i);
					} else if (ChannelDataType.qitan.name().equals(extendFields.get("type"))) {
						qitan = channelSearchResults.remove(i);
					} else if (ChannelDataType.film.name().equals(extendFields.get("type"))) {
						film = channelSearchResults.remove(i);
					}
				}
			}
			
			// 普通类别排序
			Queue<SearchResult> queue5k = new LinkedList<SearchResult>();
			Queue<SearchResult> queue2k = new LinkedList<SearchResult>();
			Queue<SearchResult> queue1k = new LinkedList<SearchResult>();
			Queue<SearchResult> queueOther = new LinkedList<SearchResult>();//小于1000分的结果
			for (int i = channelSearchResults.size()-1; i >=0; i--) {
				if (channelSearchResults.get(i).getScore() >= 20000) {
					//不做整合，在TopScoreSort中强制放到第一位
				}else if (channelSearchResults.get(i).getScore() >= 5000) {
					queue5k.add(channelSearchResults.get(i));
				}else if (channelSearchResults.get(i).getScore() >= 2000) {
					queue2k.add(channelSearchResults.get(i));
				}else if (channelSearchResults.get(i).getScore() >= 1000) {
					queue1k.add(channelSearchResults.get(i));
				} else {
					queueOther.add(channelSearchResults.get(i));
				}
			}
			while(!queue5k.isEmpty()){//>= 5000
				results.add(0, queue5k.poll());
			}
			while(!queue2k.isEmpty()){//>= 2000
				results.add(minPos(results.size(), 2), queue2k.poll());
			}
			while(!queue1k.isEmpty()){//>= 1000
				results.add(minPos(results.size(), 4), queue1k.poll());
			}
			while(!queueOther.isEmpty()){//other
				results.add(minPos(results.size(), 5), queueOther.poll());
			}
			
			//电视猫奇谈电影的排序
			if (tqf > 1) {
				int temp = last3gPos;
				if (tvcat != null) {
					if (tvcat.getScore() <= 2000) {
						temp = temp + 2;
					}
					results.add(minPos(results.size(), temp), tvcat);
				}
				if (qitan != null && tvcat != null) {
					temp = temp + 2;
					results.add(minPos(results.size(), temp), qitan);
				} else if (qitan != null) {
					if (qitan.getScore() <= 2000) {
						temp = temp + 2;
					}
					results.add(minPos(results.size(), temp++), qitan);
				}
				if (film != null && (qitan != null || tvcat != null)) {
					temp = temp + 2;
					results.add(minPos(results.size(), temp), film);
				} else if (film != null) {
					if (film.getScore() <= 2000) {
						temp = temp + 2;
					}
					results.add(minPos(results.size(), temp++), film);
				}
			}
			if (stock != null) {
				results.add(minPos(results.size(), 2), stock);
			}
//			if (gouwu != null) {
//				results.add(minPos(results.size(), 2), gouwu);
//			}

			if(teleplay != null){
				results.add(0, teleplay);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取最小位置
	 * 
	 * @return
	 */
	private static int minPos(int a, int b) {
		return a == 0 ? 0 : b < a ? b : a;
	}
}
